<template>
  <a-modal :visible="visible" :title="form.id ? '编辑' : '添加'" :width="1000" forceRender ok-text="确认" cancel-text="取消"
    @ok="onSubmit" @cancel="onClose">

  <a-tabs v-model:activeKey="activeKey">
    <a-tab-pane key="1" tab="客户信息" force-render>
    <enterpriseInfo />
    </a-tab-pane>
    <a-tab-pane key="2" tab="联系人" force-render>
    <personInfo />
    </a-tab-pane>
    <a-tab-pane key="3" tab="企业信息" force-render>
    <enterpriseDetailInfo />
    </a-tab-pane>
  </a-tabs>



  </a-modal>
</template>

<script setup>
import { message } from 'ant-design-vue';
import _, { fromPairs } from 'lodash';
import { nextTick, reactive, ref } from 'vue';
import { asApi } from '/@/api/addrstandard/as-api'
import { SmartLoading } from '/@/components/framework/smart-loading';
import { smartSentry } from '/@/lib/smart-sentry';
import DictSelectType from '/@/components/cache/dict-select-type.vue'
import { SERVICE_ID_ENUM, DICT_TYPE_ENUM } from '/@/constants/system/cache-service-id-const';
import enterpriseInfo from './enterprise-info.vue'
import enterpriseDetailInfo from './enterprise-detail-info.vue';
import personInfo from './person-info.vue';
defineExpose({
  showModal,
});
const emit = defineEmits('refresh');

const activeKey = ref('1');

// --------------------- modal 显示与隐藏 ---------------------
// 是否展示
const visible = ref(false);
const dictValueList = ref([]);

function showModal(record) {
  console.log('record', record)
  if (record) {
    Object.assign(form, record);
  }
  else {
    Object.assign(form, formDefault);
  }
  loadSelect();
  visible.value = true;
}

function onClose() {
  visible.value = false;
}

async function loadSelect() {
  let res = await asApi.findTodoTask(form);
  if (res.success)
    dictValueList.value = res.data;
}

// --------------------- 表单 ---------------------
// 省市区级联数据，实际可通过接口获取，这里简单模拟
const regionOptions = [
  {
    value: '广东省',
    label: '广东省',
    children: [
      {
        value: '佛山市',
        label: '佛山市',
        children: [
          {
            value: '高明区',
            label: '高明区'
          }
        ]
      }
    ]
  }
];

// 表单数据


//  组件
const formRef = ref();

const formDefault = {
  customerShortName: '佛山友科财税服务有限公司',
  customerType: '非代账客户',
  accountServiceStatus: '服务中',
  area: '',
  customerLevel: '',
  source: '',
  customerTag: '',
  enterpriseFullName: '佛山友科财税服务有限公司',
  department: '佛山友科财税服务有限公司',
  taxNumber: '914406050MA54UC6A69',
  customerNo: '001',
  vatType: '小规模纳税人',
  industryCategory: '租赁和商务服务业-商务服务业',
  region: ['广东省', '佛山市', '高明区'],
  detailAddress: '佛山市高明区荷城街道中山路1号春天里花园119号商铺（住所申报）',
  customerManager: '阿静',
  serviceAdvisor: '杨亚丽',
  accountant: '阿静',
  taxAccountant: '阿静',
  chargePerson: ['罗会计'],
  invoicePerson: ['罗会计'],
  auditAccountant: ['叶会计', '李会计', '阿静'],
  otherServicePerson: ['冯祖云', '小谢', '卢会计', '杨亚丽', '罗会计']
};
let form = reactive({ ...formDefault });
const rules = {
  cityCode: [{ required: true, message: '请选择城市' }],
  taskName: [{ required: true, message: '请输入任务名称' }],
  outTaskId: [{ required: true, message: '请输入外部任务ID' }],
};

function onSubmit() {
  formRef.value
    .validate()
    .then(async () => {
      SmartLoading.show();
      try {
        let res = await asApi.updateAs(form);
        message.success(res.message);
        emit('refresh');
        onClose();
      } catch (error) {
        smartSentry.captureError(error);
      } finally {
        SmartLoading.hide();
      }
    })
    .catch((error) => {
      console.log('error', error);
      message.error('参数验证错误，请仔细填写表单数据!');
    });
}



</script>

<style lang="less" scoped>
.form-width {
  width: 100% !important;
}

.footer {
  width: 100%;
  display: flex;
  justify-content: flex-end;
}

:deep(.ant-card-body) {
  padding: 10px;
}
</style>
